<template>
	<view>
		<button open-type="getUserInfo" @getuserinfo="wxLogin">点我登录</button>
	</view>
</template>

<script>
	export default {
		methods: {
			wxLogin(userInfoRes) {
				//获取用户信息
				console.log(userInfoRes)
				let {
					iv,
					rawData,
					signature,
					encryptedData
				} = userInfoRes.detail
				//先获取code
				uni.login({
					success: async (loginRes) => {
						// success:function(){
						console.log(loginRes.code);
						const code = loginRes.code

						//发请求
						let data = await this.$request({
							url: '/api/public/v1/users/wxlogin',
							method: 'POST',
							data: {
								iv,
								rawData,
								signature,
								encryptedData,
								code
							}
						})

						console.log(data.token)
						// 存token
						uni.setStorageSync('token', data.token)
						uni.setStorageSync('userInfo', userInfoRes.detail.userInfo)
						uni.showToast({
							title: '登录成功！',
							success() {
								// 提示框成功显示的时候触发
								setTimeout(() => {
									uni.navigateBack()
								}, 1500)
							}
						})
					}
				});
			}
		}
	}
</script>

<style>
</style>
